package com.hyp.pay.UI.Home.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.hyp.pay.R;
import com.hyp.pay.UI.Home.presenter.IdCardPresenter;
import com.hyp.pay.UI.Home.presenter.contract.IdCardContract;
import com.hyp.pay.base.BaseActivity;
import com.hyp.pay.http.HyqApis;
import com.hyp.pay.model.UpLodAttion;
import com.hyp.pay.model.UplodIdcard;
import com.hyp.pay.utils.ImageLoader;
import com.hyp.pay.utils.SPUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class IDCardActivity extends BaseActivity<IdCardPresenter> implements IdCardContract.View {


    @BindView(R.id.textView)
    TextView mTextView;
    @BindView(R.id.idcard_zheng)
    ImageView mIdcardZheng;
    @BindView(R.id.idcard_fan)
    ImageView mIdcardFan;
    @BindView(R.id.sure_button)
    Button mSureButton;

    private static final int sRequestPhotoCode = 101;
    private static final int sRequestPhotoCode2 = 102;

    private String uplodPath,uplodPath2;

    @Override
    protected void initInject() {

        getActivityComponent().inject(this);

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_idcard;
    }

    @Override
    protected void initView() {

    }

    @OnClick({R.id.sure_button,R.id.idcard_zheng,R.id.idcard_fan,R.id.back})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.sure_button:
                Log.i("olj",uplodPath);
                Log.i("olj",uplodPath2);
                mPresenter.getUploadIDCard(uplodPath,uplodPath2, Integer.parseInt(SPUtil.getUserid()));

                break;
            case R.id.idcard_zheng:
                startActivityForResult(new Intent(this, RectCameraActivity.class), sRequestPhotoCode);
                break;
            case R.id.idcard_fan:
                startActivityForResult(new Intent(this, RectCameraActivity.class), sRequestPhotoCode2);
                break;
            case R.id.back:
                finish();
                break;

        }

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == sRequestPhotoCode && resultCode == Activity.RESULT_OK) {
            Log.i("olj",data.getStringExtra("filePath"));
//            ImageLoader.loadImage(mImageLoader, mIdcardZheng,  WedApis.HOST + data.getOriginal_url());
            mPresenter.getUploadFileIDCard(data.getStringExtra("filePath"),Integer.parseInt(SPUtil.getUserid()));
        }
        if (requestCode == sRequestPhotoCode2 && resultCode == Activity.RESULT_OK) {
            mPresenter.getUploadFileIDCard2(data.getStringExtra("filePath"), Integer.parseInt(SPUtil.getUserid()));
        }

    }


    @Override
    public void showUplod(UplodIdcard data) {
        if (data.getCode().equals("200")){
            uplodPath=data.getPath();
            ImageLoader.loadImage(mImageLoader, mIdcardZheng,  HyqApis.HOST +data.getPath());
            Toast.makeText(mContext, "身份证正面上传成功", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void showUplod2(UplodIdcard data) {
        if (data.getCode().equals("200")){
            uplodPath2=data.path;

            ImageLoader.loadImage(mImageLoader, mIdcardFan,  HyqApis.HOST +data.getPath());
            Toast.makeText(mContext, "身份证正面上传成功", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void showUplodAttion(UpLodAttion data) {
        Log.i("olj",data.getCode());
        if (data.getCode().equals("200")){
            getFinsh();
            Toast.makeText(mContext, data.getMsg(), Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(mContext,"请重新上传", Toast.LENGTH_SHORT).show();
        }

    }

    private void getFinsh() {
        finish();
    }

    @Override
    public void showError(String msg) {

    }


}
